#include"head.h"
#include"log.h"

int continue_trans(MYSQL*connection,int netfd,const char* hash)
{
    char addrs[128];
    sprintf(addrs,"../file/%s",hash);
    printf("trans:%s\n",addrs);
    int file_fd=open(addrs,O_RDWR|O_APPEND|O_CREAT,0666);
    if(file_fd==-1)
    {
        ERROR_LOG("文件打开失败\n");
    }
    struct stat stat;
    fstat(file_fd,&stat);
    size_t file_size=stat.st_size;
    printf("file_size:%d\n",(int)file_size);
    send(netfd,&file_size,sizeof(file_size),0);

    size_t recvbyte ;
    recv (netfd,&recvbyte,sizeof(int),0);
    printf("recvbyte:%d\n",(int)recvbyte);

    size_t read=0;
    char buf[4096];
    while(read<recvbyte)
    {
        int ret;
        ret=recv(netfd,buf,sizeof(buf),0);
        printf("%s",buf);
        if(ret==0)
        {
            ERROR_LOG("客户端断开\n");
        }
        read += ret;
        write(file_fd,buf,ret);
    }
    if(read==recvbyte)
    {
        change_file_transfer(connection,hash);
    }
    printf("trans done\n");

        close(file_fd);
}
        
        


